<template>
  <div class="tab-box">
    <div class="tab-item"
         v-for="item in tabList"
         :key="item.id"
         :value="item.id"
         :class="{ 'tab-item-active': item.id === activeKey }"
         @click="changeTab(item)"
         :style="{ height: height, 'line-height': height, 'font-size': fontSize }">
      <slot name="title"
            :value="item">
        <span>{{ item.name }}</span>
      </slot>
    </div>
  </div>
</template>

<script setup>
import { ref, watch } from "vue";

defineOptions({
  name: "vptSelectBlock",
});

const props = defineProps({
  height: {
    type: String,
    default: () => {
      return "0.24rem";
    },
  },
  fontSize: {
    type: String,
    default: () => {
      return "0.14rem";
    },
  },
  // tab项列表
  tabList: { type: Array, default: () => [] },
  // 默认选中
  defaultSelect: { type: [Number, String], default: undefined },
});

const emit = defineEmits(["changeTab"]);

const activeKey = ref();

watch(
  () => props.defaultSelect,
  () => {
    activeKey.value =
      props.defaultSelect || (props.tabList && props.tabList[0].id);
  },
  { deep: true, immediate: true }
);

const changeTab = (item) => {
  emit("changeTab", item);
};
</script>

<style lang="less" scoped>
.tab-box {
  // width: 100%;
  display: flex;
  justify-content: flex-end;
  margin-left: 0.2rem;

  .tab-item {
    // height: 0.48rem;
    padding-left: 0.12rem;
    padding-right: 0.12rem;
    // line-height: 0.48rem;
    cursor: pointer;
    text-align: center;
    background: #F2F3F5;
    border: 0.01rem solid #F2F3F5;

    :deep(span) {
      // font-family: PingFangSC-SNaNpxibold;
      font-weight: 400;
      // font-size: 0.26rem;
      color: #4E5969;
      opacity: var(--color-noraml-opacy);
    }

    // &:not(:first-child) {
    //   margin-left: -1px;
    //   border-left-color: transparent;
    // }
  }

  .tab-item-active {
    background: #fff;
    border: 0.01rem solid #E5E6EB;
    // text-shadow: 0 4px 14px #006aff;
    // font-family: PingFangSC-SNaNpxibold;
    font-weight: 600;

    :deep(span) {
      color: #1D2129;
      opacity: 1;
    }
  }
}
</style>